<?php
/**
 * Task for creating project
 *
 * @author Andrey Kucherenko (kucherenko.andrey@gmail.com)
 * @version 0.3
 * @task create
 */
class CompleXml_Cli_Task_Create extends CompleXml_Cli_Task {

    private $_settings = array();

    public function  __construct($argv) {
        $options = array(
            '--full'=>array(
            'aliases'=>array('-f'),
            ),
            '--name'=>array(
            'position'=>3,
            'type'=>'String'
            ),
            '--with-lib'=>array()
        );
        parent::__construct($options, $argv);
    }

    public function init() {
        $this->_settings = CompleXml_Config::readComponentSettings(__CLASS__);
    }
    /**
     * Create project in current directory
     * @example create project name
     * @example create project --name=projectname
     * @example create project --name projectname
     * @action project
     */
    public function project() {
        $name = $this->Getopt->getValue('--name');
        if (is_null($name)) {
            throw new CompleXml_Cli_Exception('Enter name of project');
        }
        $project_directory = APPLICATION_HOME.DIRECTORY_SEPARATOR.$name;
        if (file_exists($project_directory)&& is_dir($project_directory)) {
            throw new CompleXml_Cli_Exception('Folder "'.$project_directory.'" already exists');
        }
        if (!mkdir($project_directory, 0755)) {
            throw new CompleXml_Cli_Exception('Can\'t create folder named "'.$name.'"');
        }
        CompleXml_Log::info('>Folder "'.$project_directory.'" created');
        $this->_copy($this->_settings['sandbox'], $project_directory);
        CompleXml_Log::info('>Sandbox copied to "'.$project_directory.'"');
        chmod($project_directory.DIRECTORY_SEPARATOR.'cx', 0777);
        chmod($project_directory.DIRECTORY_SEPARATOR.'logs', 0777);
        chmod($project_directory.DIRECTORY_SEPARATOR.'cache', 0777);
        if ($this->Getopt->getValue('--with-lib')) {
            $this->_copy(CompleXml_Loader::getLibraryPath(), $project_directory.DIRECTORY_SEPARATOR.'lib');
            CompleXml_Log::info('>Library copied to "'.$project_directory.DIRECTORY_SEPARATOR.'lib"');
        }
        CompleXml_Log::notice('Project created');
        CompleXml_Log::notice('For manage project, use "'.$project_directory.DIRECTORY_SEPARATOR.'cx" script');
    }

    private function _copy($source, $dest, $folderPermission=0755, $filePermission=0644) {
        $result=false;

        if (is_file($source)) {
            if(is_dir($dest)) {
                if ($dest[strlen($dest)-1]!='/')
                    $__dest=$dest."/";
                $__dest .= basename($source);
            }
            else {
                $__dest=$dest;
            }
            $result=copy($source, $__dest);
            chmod($__dest,$filePermission);
        }
        elseif(is_dir($source)) {
            if(!is_dir($dest)) {
                @mkdir($dest,$folderPermission);
                chmod($dest,$folderPermission);
            }
            if ($source[strlen($source)-1]!='/')
                $source=$source."/";
            if ($dest[strlen($dest)-1]!='/')
                $dest=$dest."/";

            $return = true;
            $dirHandle=opendir($source);
            while($file=readdir($dirHandle)) {
                if($file!="." && $file!="..") {
                    $result=$this->_copy($source.$file, $dest.$file, $folderPermission, $filePermission);
                }
            }
            closedir($dirHandle);
        }
        else {
            $result=false;
        }
        return $result;
    }
}


